package icraf.carbon.gui.chart;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.RoundRectangle2D;

import javax.swing.JPanel;

public class ShapePreviewPanel extends JPanel {
	//public static final int LINE_SETTING = 1, SHAPE_SETTING = 2;

	private static final long serialVersionUID = -6576652428222722701L;
	int borderSpace = 30;//pixel
	Color colorSpace = Color.white;
	
	public Color getColorSpace() {
		return colorSpace;
	}

	public void setColorSpace(Color colorSpace) {
		this.colorSpace = colorSpace;
	}

	public int getBorderSpace() {
		return borderSpace;
	}

	public void setBorderSpace(int borderSpace) {
		this.borderSpace = borderSpace;
	}

	private int borderOut = 0;
	private int settingType = ShapeSettingPanel.SHAPE_SETTING;
	
	public int getSettingType() {
		return settingType;
	}

	public void setSettingType(int settingType) {
		this.settingType = settingType;
	}

	private ShapeFormat shapeSetting = new ShapeFormat();
	private BasicStroke stroke;

	public ShapeFormat getShapeSetting() {
		return shapeSetting;
	}

	@Override
	public void paint(Graphics g) {

		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);

		try {
			stroke = new BasicStroke(shapeSetting.lineSize,
					BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 10.0f,
					shapeSetting.dashPattern, 0.0f);
			g2.setStroke(stroke);
		} catch (Exception e) {
			stroke = new BasicStroke(shapeSetting.lineSize,
					BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 10.0f, null,
					0.0f);
			g2.setStroke(stroke);
		}

		Dimension dim = getSize();
		int h = (int) dim.getHeight();
		int w = (int) dim.getWidth();
		g2.setColor(colorSpace);
		g2.fillRect(borderOut, borderOut, w - 2 * borderOut, h - 2 * borderOut);
		if (settingType == ShapeSettingPanel.LINE_SETTING) {
			if (shapeSetting.lineSize > 0) {
				g2.setColor(shapeSetting.lineColor);
				g2.drawLine(borderSpace, h / 2, w - borderSpace, h / 2);
			}
		} else if (settingType == ShapeSettingPanel.SHAPE_SETTING || settingType == ShapeSettingPanel.SHAPE_REC_SETTING || settingType == ShapeSettingPanel.COLOR_SETTING) {
			RoundRectangle2D rec = new RoundRectangle2D.Float(borderSpace, borderSpace, w
					- 2 * borderSpace, h - 2 * borderSpace, shapeSetting.corner,
					shapeSetting.corner);
			g2.setColor(shapeSetting.fillColor);
			g2.fill(rec);
			if (shapeSetting.lineSize > 0) {
				g2.setColor(shapeSetting.lineColor);
				g2.draw(rec);
			}

		}
	}

	public void setShapeSetting(ShapeFormat shapeSetting) {
		this.shapeSetting = shapeSetting;
	}
}
